package com.example.service;

import com.example.dao.ChongzhirecordInfoDao;
import com.example.dao.SigninusersInfoDao;
import com.example.entity.*;
import com.example.entity.ChongzhirecordInfo;
import com.example.entity.SigninusersInfo;
import com.example.vo.ChongzhirecordInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class ChongzhirecordInfoService {

    @Resource
    private ChongzhirecordInfoDao chongzhirecordInfoDao;
	@Resource
    private SigninusersInfoService signinusersInfoService;
    
	
	@Resource
    private SigninusersInfoDao signinusersInfoDao;

    public ChongzhirecordInfo add(ChongzhirecordInfo chongzhirecordInfo) {
        //shangxchxuantupxian
        // 唯一校验
        
      // chongzhirecordInfo.setAccount((double) 0);
	  SigninusersInfo signinusersInfo = signinusersInfoDao.selectByUsername(chongzhirecordInfo.getUsername());
        signinusersInfo.setYue(String.valueOf(Integer.valueOf(signinusersInfo.getYue())+Integer.valueOf(chongzhirecordInfo.getChongzhiamount())));
        signinusersInfoDao.updateByPrimaryKeySelective(signinusersInfo);
        chongzhirecordInfoDao.insertSelective(chongzhirecordInfo);
        return chongzhirecordInfo;
    }
	
	public List<Map<String,String>> findByDiqu() {
        return chongzhirecordInfoDao.findByDiqu();
    }

    public void delete(Long id) {
        chongzhirecordInfoDao.deleteByPrimaryKey(id);
    }

    public void update(ChongzhirecordInfo chongzhirecordInfo) {
        //shangxchxuantupxian
		//youdianzan
        chongzhirecordInfoDao.updateByPrimaryKeySelective(chongzhirecordInfo);
    }

    public ChongzhirecordInfo findById(Long id) {
        return chongzhirecordInfoDao.selectByPrimaryKey(id);
    }
	
    public List<ChongzhirecordInfoVo> findAll() {
        return chongzhirecordInfoDao.findByNames("all");
    }

    public PageInfo<ChongzhirecordInfoVo> findPage(String names, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<ChongzhirecordInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        if (user.getLevel().equals("管理员")) {all = chongzhirecordInfoDao.findByNames(names);}
		if (user.getLevel().equals("注册用户")) {all = chongzhirecordInfoDao.findByNameslist2(names,user.getUsername()); }
        
        
        return PageInfo.of(all);
    }
	
	public PageInfo<ChongzhirecordInfoVo> findPageqt(String names, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<ChongzhirecordInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        all = chongzhirecordInfoDao.findByNames(names);
		if (user.getLevel().equals("注册用户")) {all = chongzhirecordInfoDao.findByNameslist2(names,user.getUsername()); }
        
	
	
        
        return PageInfo.of(all);
    }

   // public ChongzhirecordInfoVo findByUserName(String name) {
//        return chongzhirecordInfoDao.findByUsername(name);
//    }

	//yoxulogindenxglu
	
    public void changeStatus(Long id) {
        ChongzhirecordInfo chongzhirecordInfo = chongzhirecordInfoDao.selectByPrimaryKey(id);
        if(chongzhirecordInfo.getStatus().equals("是")){
            chongzhirecordInfo.setStatus("否");
            chongzhirecordInfoDao.updateByPrimaryKey(chongzhirecordInfo);
        }else if(chongzhirecordInfo.getStatus().equals("否")){
            chongzhirecordInfo.setStatus("是");
            chongzhirecordInfoDao.updateByPrimaryKey(chongzhirecordInfo);
        }
    }
	public void changeIszf(Long id) {
       ChongzhirecordInfo chongzhirecordInfo = chongzhirecordInfoDao.selectByPrimaryKey(id);
        if(chongzhirecordInfo.getIszf().equals("是")){
            chongzhirecordInfo.setIszf("否");
            chongzhirecordInfoDao.updateByPrimaryKey(chongzhirecordInfo);
        }else if(chongzhirecordInfo.getIszf().equals("否")){
            chongzhirecordInfo.setIszf("是");
            chongzhirecordInfoDao.updateByPrimaryKey(chongzhirecordInfo);
        }
    }
	//youtixing
	
	
	
	public List<ChongzhirecordInfo> getBysigninusersUsername(String username) {
    SigninusersInfo signinusersInfo = signinusersInfoService.findByUsername(username);
    return chongzhirecordInfoDao.findByusername(signinusersInfo);
    }
    
	
	//xiugaimimazys
	
}
